package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.aliyuncs.exceptions.ClientException;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.OrderlistDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderList;
import com.itheima.pojo.Setmeal;
import com.itheima.service.OrderlistService;

import com.itheima.utils.SMSUtils;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.factory.annotation.Autowired;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;


@Service(interfaceClass = OrderlistService.class)
public class OrderlistServiceImpl implements OrderlistService {

    @Autowired    //service和dao在一个项目中，直接依赖注入
    private OrderlistDao orderlistDao;

    /**
     * 分页查询
     *
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        //传入当前页面，每页显示行数得到分页
        PageHelper.startPage(currentPage, pageSize);
        if (queryString != null) {
            queryString = "%" + queryString + "%";
        }
        Page<OrderList> orderLists = orderlistDao.selectqueryString(queryString);
        List<OrderList> result = orderLists.getResult();
        long total = orderLists.getTotal();
        return new PageResult(total, orderLists);
    }

    /**
     * 新增预约信息（电话预约）
     *
     * @param orderList
     */
    @Override
    public void add(OrderList orderList,Integer[] setmealIds) {
        //先查询一次是否有条件全部符合的
        Integer selectCondition = orderlistDao.selectCondition(orderList);
        if (selectCondition == null || selectCondition == 0) {
            //添加member数据
            orderlistDao.addMember(orderList);
            //将memberid添加order表中,他会将主键id自动返回给封装好的对象，而不是返回值获取
            orderList.setOrderStatus("未预约");
            Date orderDate = orderList.getOrderDate();
//            for (Integer setmealId : setmealIds) {
//                orderList.setSetmealId(setmealId);
                orderlistDao.addOrder(orderList);
//            }
        }
        new RuntimeException("此数据已存在于数据库中");
    }

    /**
     * 发送预约短信
     *
     * @param id
     */
    @Override
    public void sendMessage(Integer id) {
        String PhoneNumber = orderlistDao.selectPhoneNumber(id);//电话号码
        Date OrderData = orderlistDao.selectOrderData(id);//预约日期
        String userName=orderlistDao.selectUserName(id);//用户名
        String OrderDate = new SimpleDateFormat("yyyy年MM月dd日").format(OrderData);
        //调用阿里云工具类进行发送
        try {
            SMSUtils.sendShortMessage(SMSUtils.DATE_OF_RESERVATION,PhoneNumber,userName,OrderDate);
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }

    /**
     * 修改预约状态
     *
     * @param id
     */
    @Override
    public void updateOrderStatus(Integer id) {
        orderlistDao.updateStatus(id);
    }

    /**
     * 取消预约并发送短信
     * @param id
     */
    @Override
    public void deletOrder(Integer id) {
        String PhoneNumber = orderlistDao.selectPhoneNumber(id);//电话号码
        Date OrderData = orderlistDao.selectOrderData(id);//预约日期
        String userName=orderlistDao.selectUserName(id);//用户名
        String OrderDate = new SimpleDateFormat("yyyy年MM月dd日").format(OrderData);
        //调用阿里云工具类进行发送
        try {
            SMSUtils.sendShortMessage(SMSUtils.CANCEL_THE_RESERVATION,PhoneNumber,userName,OrderDate);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        orderlistDao.deletOrder(id);
    }

    @Override
    public void BatchDeletOrder(OrderList[] orderList) {
        if (orderList != null || orderList.length > 0) {
            for (OrderList list : orderList) {
                //遍历得到前台传过来的id
                int id = list.getId();
                orderlistDao.deletOrder(id);
            }
        }
    }

    @Override
    public List<Setmeal> findsetmeal() {
        return orderlistDao.selectSetMeal();
    }
}
